/// <reference path="../routing/HttpRequest.js" />
/// <reference path="../utilities/FXDom.js" />
/// <reference path="../utilities/Markup.js" />
/// <reference path="../utilities/Post.js" />
/// <reference path="../utilities/Xpcom.js" />
///--------------------------------------------------------------------
/// <file name="QuickQuote.js">
///   Allows posts to be quick quoted to the quick reply text area.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.QuickQuote = function(_$, htmlDocument, threadData) {
    /// <summary>Initializes a new instance of the QuickEdit class.</summary>
    /// <param name="_$" type="FXDom">
    ///   The FXDom selector that allows this QuickEdit instance
    ///   to select DOM elements from the page itself.
    /// </param>
    /// <param name="htmlDocument" type="Document">
    ///   The HTML Document object for the current page.
    /// </param>
    /// <param name="threadData">
    ///   Contains various data about the thread as well
    ///   as features added on the thread itself, like Quick Edit.
    /// </param>
    
    this._$ = _$;
    this._htmlDocument = htmlDocument;
    this._threadData = threadData;
};

QuickQuote.prototype = {
    _onQuickQuoteClicked: function(event, post) {
        /// <summary>Called when the quick quote link is clicked.</summary>
        /// <param name="post" type="Post">
        ///   The Post object on which the quick quote link is clicked.
        /// </param>

        var that = this;
        function markupReceived(markup) {
            var markup =
                "[quote=" +
                post.getAuthor() +
                "|message:" +
                post.getMessageId() +
                "]\n" +
                markup +
                "\n[/quote]\n\n";
            
            Markup.addText(markup, that._$("#namfox-quick-reply-textarea").get(0));
        }

        post.getMarkup(markupReceived);
    },

    attach: function() {
        /// <summary>
        ///   Renders the quick quote UI on the view thread page.
        /// </summary>
        
        // If the thread does not have a quick reply link and the user
        // is not a moderator, then don't add quick quote.
        if (this._$("a[name=quickreply_link]").length === 0 &&
            !this._threadData.isUserModerator()) {
            return;
        }
        
        var posts = this._threadData.getPosts();
        var that = this;
        
        $FX.each(
            posts,
            function(i, post) {
                that.attachSingle.call(that, post);
            }
        );
    },
    
    attachSingle: function(post) {
        /// <summary>
        ///   Renders the quick quote UI on the view thread page.
        /// </summary>
        /// <param name="post" type="Post">
        ///   The Post object on which to bestow quick quote functionality.
        /// </param>
        
        var moderationQueueStatus = post.getModerationQueueStatus();
                
        if (moderationQueueStatus.view === ViewStatus.InView ||
            moderationQueueStatus.moderator === ModeratorStatus.Moderator) {
            
            var linkRow = $FX(post.linkRow);
            linkRow.find("img[src$=replyquote.gif]:even").
                attr("src", "chrome://namfox/skin/posting/quickquote.gif");
                
            linkRow.find("a:contains(quick quote)").
                after('<a class="namfox-post-link" name="namfox-quick-quote-link">quick quote</a>').
                remove();
                
            linkRow.find("a[name=namfox-quick-quote-link]").click(
                $.createCallback(this._onQuickQuoteClicked, this, post)
            );
        }
    }
};

}});